package com.kfm.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.kfm.commons.Constant;
import lombok.Data;

import java.io.File;
import java.util.Date;

@Data
public class FileVo {

    private File file;
    private String name;
    private long size;
    private String unit;
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm", timezone = "GMT+8")
    private Date lastModified;

    public FileVo(File file) {
        this.file = file;
        this.name = file.getName();
        this.size = file.length();
        this.lastModified = new Date(file.lastModified());
        this.unit = Constant.UNITS[this.getIndex(this.size)];
    }

    private int getIndex(long size) {
        int index = 0;
        while (size / 1024 > 0) {
            index ++;
            size = size / 1024;
        }
        this.size = size;
        return index;
    }
}
